package com.nanyang.tms.controller.saleproduct;

import javax.validation.Valid;

import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.nanyang.common.core.annotation.BindParam;
import com.nanyang.common.core.dict.CommonEnum;
import com.nanyang.common.core.domain.ApiResult;
import com.nanyang.common.core.exception.ServiceException;
import com.nanyang.common.core.page.PageBean;
import com.nanyang.tms.api.dto.request.saleproduct.SaleProductQueryRequest;
import com.nanyang.tms.api.dto.request.saleproduct.SaleProductUpdateStateRequest;
import com.nanyang.tms.api.dto.request.saleproduct.add.SaleProductBaseAddRequest;
import com.nanyang.tms.api.dto.request.saleproduct.update.SaleProductBaseUpdateRequest;
import com.nanyang.tms.api.dto.response.saleproduct.SaleProductBaseResponse;
import com.nanyang.tms.api.dto.response.saleproduct.SaleProductPageKeyResponse;
import com.nanyang.tms.api.dto.response.saleproduct.SaleProductPageResponse;
import com.nanyang.tms.api.service.saleproduct.SaleProductRpcService;

@RestController
@RequestMapping("/saleproduct")
public class SaleProductController {

    @Reference(version = "1.0.0", timeout = 10000, check = false)
    private SaleProductRpcService saleProductRpcService;

    /**
     * 列表
     * @param param
     * @return
     */
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    public ApiResult<PageBean<SaleProductPageResponse>> listSaleProduct(@RequestBody SaleProductQueryRequest param) {
        ApiResult<PageBean<SaleProductPageResponse>> apiResult = saleProductRpcService.listSaleProduct(param);
        if (apiResult.getCode() == CommonEnum.SYSTEM_EXCEPTION.getCode()) {
            throw new ServiceException(CommonEnum.SYSTEM_EXCEPTION.getCode(), apiResult.getMessage());
        }
        return apiResult;
    }

    /**
     * 列表
     * @param param
     * @return
     */
    @RequestMapping(value = "/listByKey", method = RequestMethod.POST)
    public ApiResult<PageBean<SaleProductPageKeyResponse>> listSaleProductByKey(@RequestBody SaleProductQueryRequest param) {
        ApiResult<PageBean<SaleProductPageKeyResponse>> apiResult = saleProductRpcService.listSaleProductByKey(param);
        if (apiResult.getCode() == CommonEnum.SYSTEM_EXCEPTION.getCode()) {
            throw new ServiceException(CommonEnum.SYSTEM_EXCEPTION.getCode(), apiResult.getMessage());
        }
        return apiResult;
    }
    /**
     * 添加
     * @param param
     * @return
     */

    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @BindParam
    public ApiResult<Boolean> saveProduct(@BindParam @RequestBody @Valid SaleProductBaseAddRequest param) {
        ApiResult<Boolean> apiResult = saleProductRpcService.saveSaleProduct(param);
        if (apiResult.getCode() == CommonEnum.SYSTEM_EXCEPTION.getCode()) {
            throw new ServiceException(CommonEnum.SYSTEM_EXCEPTION.getCode(), apiResult.getMessage());
        }
        return apiResult;
    }
    /**
     * 修改
     * @param param
     * @return
     */

    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @BindParam
    public ApiResult<Boolean> saveProduct(@BindParam @RequestBody @Valid SaleProductBaseUpdateRequest param) {
        ApiResult<Boolean> apiResult = saleProductRpcService.updateSaleProduct(param);
        if (apiResult.getCode() == CommonEnum.SYSTEM_EXCEPTION.getCode()) {
            throw new ServiceException(CommonEnum.SYSTEM_EXCEPTION.getCode(), apiResult.getMessage());
        }
        return apiResult;
    }
    /**
     * 禁用
     * @param param
     * @return
     */
    @RequestMapping(value = "/disable", method = RequestMethod.POST)
    @BindParam
    public ApiResult<Boolean> disableState(@BindParam @RequestBody @Valid SaleProductUpdateStateRequest param) {
        ApiResult<Boolean> apiResult = saleProductRpcService.disableState(param);
        if (apiResult.getCode() == CommonEnum.SYSTEM_EXCEPTION.getCode()) {
            throw new ServiceException(CommonEnum.SYSTEM_EXCEPTION.getCode(), apiResult.getMessage());
        }
        return apiResult;
    }

    /**
     * 启用
     * @param param
     * @return
     */
    @RequestMapping(value = "/enable", method = RequestMethod.POST)
    @BindParam
    public ApiResult<Boolean> enableState(@BindParam @RequestBody @Valid SaleProductUpdateStateRequest param) {
        ApiResult<Boolean> apiResult = saleProductRpcService.enableState(param);
        if (apiResult.getCode() == CommonEnum.SYSTEM_EXCEPTION.getCode()) {
            throw new ServiceException(CommonEnum.SYSTEM_EXCEPTION.getCode(), apiResult.getMessage());
        }
        return apiResult;
    }

    /**
     * <p>listSupplier</p>
     * <p>Description:查询根据编码</p>
     *
     * @param saleProductCode
     * @return
     */
    @RequestMapping(value = "/selectByCode/{saleProductCode}", method = RequestMethod.GET)
    public ApiResult<SaleProductBaseResponse> selectByCode(@PathVariable("saleProductCode") String saleProductCode) {
        ApiResult<SaleProductBaseResponse> apiResult = saleProductRpcService.selectByCode(saleProductCode);
        if (apiResult.getCode() == CommonEnum.SYSTEM_EXCEPTION.getCode()) {
            throw new ServiceException(CommonEnum.SYSTEM_EXCEPTION.getCode(), apiResult.getMessage());
        }
        return apiResult;
    }
}
